<template>
    <div class="addProject business-workAdd">
        <div class="addForm zll-form">
            <div class="tableList">
                <table border="1">
                    <tr>															
                        <th>日期</th>
                        <th>日发电量（MWh）</th>
                        <th>CO2减排（吨）</th>
                        <th>等效绿植（棵）</th>
                        <th>自用电量（MWh）</th>
                        <th>余电电量（MWh）</th>
                    </tr>
                    <tr class="add_Table">
                        <td><el-input v-model="monthDay" placeholder="" class="text_center" clearable disabled></el-input></td>
                        <td><el-input v-model="addForm.powerGeneration" placeholder="" class="text_center" clearable :disabled="type == 'see'" @change="autoGenerate"></el-input></td>
                        <td><el-input v-model="addForm.co2" placeholder="" class="text_center" clearable disabled></el-input></td>
                        <td><el-input v-model="addForm.equivalentPlant" placeholder="" class="text_center" clearable disabled></el-input></td>
                        <td><el-input v-model="addForm.selfConsumption" placeholder="" class="text_center" clearable disabled></el-input></td>
                        <td><el-input v-model="addForm.powerSurplus" placeholder="" class="text_center" clearable disabled></el-input></td>
                    </tr>
                </table>
            </div>
        </div>
  </div>
</template>

<script>
export default {
	props: ['editData','type','execYear','execMoth','recordDay'],
    data(){
        return {
            addForm: {
				id:'',
				recordYear:'',
				recordMonth:'',
				recordDay:'',
				powerGeneration:'',
				co2:'',
				equivalentPlant:'',
				selfConsumption:'',
				powerSurplus:'',
				status:'0',
            },

			dataIsValid:true,

			monthDay:'',

			count:0,
        }
    },
    methods: {
		submitForm() {

			let self = this;


			self.operate();

        },

		autoGenerate(){
			let self = this;
			self.addForm.co2 = (0.95*self.addForm.powerGeneration).toFixed(3);
			self.addForm.equivalentPlant = (0.56*self.addForm.powerGeneration).toFixed(3);
			self.addForm.selfConsumption = (0.69*self.addForm.powerGeneration).toFixed(3);

			self.addForm.powerSurplus = (self.addForm.powerGeneration - self.addForm.selfConsumption).toFixed(3);

		},


		operate(){
			let self = this;
			let url = self.addForm.id ? '/jinl/photovoltaic/updatePhotovoltaic' : '/jinl/photovoltaic/addPhotovoltaic';
			// if (!self.addForm.id){
			// 	self.addForm.recordYear = this.execYear;
			// 	self.addForm.recordMonth = this.execMoth;
			// }

			if (this.count == 0){
				self.count++;
				self.$http({
					url:url,
					method:"post",
					data: JSON.stringify(self.addForm),
					dataType: "json",
					contentType: "application/json"
				}).then(resp => {
					if (resp.success) {
						self.$message.success('操作成功');
						self.$emit('refreshData');
						return;
					}
					self.$message.error(resp.message);
					self.count--;
				})
			}
		},

    },

	watch: {
    	editData(val) {

    		if (Object.getOwnPropertyNames(val).length > 1){
    			this.addForm.id = val.id;
    			this.addForm.powerGeneration = val.powerGeneration;
    			this.addForm.co2 = val.co2;
    			this.addForm.equivalentPlant = val.equivalentPlant;
    			this.addForm.selfConsumption = val.selfConsumption;
    			this.addForm.powerSurplus = val.powerSurplus;
    			this.addForm.recordDay = val.recordDay;
    			this.addForm.recordYear = val.recordYear;
    			this.addForm.recordMonth = val.recordMonth;
				this.monthDay = val.monthDay;
			}else {
				// this.recordDay = parseInt(this.recordDay)+1;
				this.addForm.recordYear = this.execYear;
				this.addForm.recordMonth = this.execMoth;
				this.addForm.recordDay = this.recordDay;
				this.monthDay = this.execMoth+"-"+this.recordDay;

			}
		}
	}
}
</script>

<style lang="scss" scoped>
    @import "@/style/dialog.scss";
</style>